﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GestoreSuperProgetto.Model;

namespace GestoreSuperProgetto
{
    public partial class UsersManagment : Form
    {
        private bool newItem = false;
        private int numOfAdmin = 0;

        public UsersManagment()
        {
            InitializeComponent();
        }

        private void UsersManagment_Load(object sender, EventArgs e)
        {
            var man = (Owner as StartForm).DataManager;

            updateListUser();
        }

        private void dataGridView_SelectionChanged(object sender, EventArgs e)
        {
            var man = (Owner as StartForm).DataManager;            

            if(usersDataGridView.SelectedRows.Count>0)
            {
                USER user = (usersDataGridView.SelectedRows[0].DataBoundItem as USER);

                userEditor.ActiveUser = user;

                if (numOfAdmin == 1 && user.ISADMIN==1)
                    deleteBtn.Enabled = false;
                else
                    deleteBtn.Enabled = true;

                user.PERMISSIONS.Load();

                pERMISSIONSBindingSource.DataSource = user.PERMISSIONS;
            }
        }

        private void saveBtn_Click(object sender, EventArgs e)
        {
            var man = (Owner as StartForm).DataManager;
                        
            if (newItem)
            {
                USER user = userEditor.CreateNewUser();
                //user.ID = man.GetUserNextID();
                newItem = false;

                man.AddToUSERS(user);
                man.SaveChanges();
                
                //object obj = man.CreateNewEntity(user);
                //user.ID = (int)((object[])obj)[0];
                //man.CommitAndClose();
                //man.OpenDatabaseSession();
                foreach (SUPERPROJECT sprj in man.SUPERPROJECTS)
                {
                    PERMISSION perms = new PERMISSION() {
                        PREAD = 1,
                        PWRITE = 0,
                        SUPERPROJECT = sprj,
                        USER = user,
                    };
                    //perms.ID = man.GetPermissionNextID();
                    

                    man.AddToPERMISSIONS(perms);
                }

                man.SaveChanges();
            }
            else
            {
                USER user = userEditor.ActiveUser;
                if (userEditor.IsModified)
                {
                    userEditor.UpdateObject();
                }

                man.SaveChanges();
            }

            updateListUser();
        }

        private void newUserBtn_Click(object sender, EventArgs e)
        {
            newItem = true;
            userEditor.ResetValue();
        }

        private void deleteBtn_Click(object sender, EventArgs e)
        {
            var man = (Owner as StartForm).DataManager;
            USER user = (usersDataGridView.SelectedRows[0].DataBoundItem as USER);

            man.DeleteObject(user);
            man.SaveChanges();

            updateListUser();
        }

        private void updateListUser()
        {
            var man = (Owner as StartForm).DataManager;

            var usersCount = (from user in man.USERS.Include("PERMISSIONS").Include("PERMISSIONS.SUPERPROJECT")
                             where user.ISADMIN==1
                             select user).Count();

            numOfAdmin = usersCount;

            userBindingSource.DataSource = man.USERS;            
        }

        private void permissionsDataGridView_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
        }
    }
}
